
/**
 * Created by wuye on 2017/12/11.
 */
var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url: "nourl"
        }
    }
};
var ztree;

var vm = new Vue({
    el: '#rrapp',
    data: {
        // 显示添加章节的dom节点
        showDom: 'addOrEdit2',
        q: {
            bookname: null
        },
        showList: true,
        title: '创建新书',
        updateBookId:null,
        book: {
            issanjie: true,
            iswuxia: true,
            isxianjie: true,
            isqihuan: true,
            isgongting: true,
            isxianyan: true,
            isjingxuan: true,
            isremen: true,
            isserialize: true,
            picurl: null,
            categoryid: '',
            categoryname:null,
            id: null,
            bookname: null,
            ispay: false,
            origin:null,
            impower:0,
            publish:1,
            isGame:0,
        },

        book2: {
            issanjie: true,
            iswuxia: true,
            isxianjie: true,
            isqihuan: true,
            isgongting: true,
            isxianyan: true,
            isjingxuan: true,
            isremen: true,
            isserialize: true,
            picurl: null,
            categoryid: '',
            id: null,
            bookname: null,
            ispay: false,
            origin:null,
            isGame:0,
        },
        section: {
            sectionname: null,
            x: null,
            z: null,
        },
        sels: [
            // {value:"1",week:"一"},
            // {value:"2",week:"二"},
            // {value:"3",week:"三"},
            // {value:"4",week:"四"}
        ],
        imgUrl:'../../img/zanwu.png',
        isEdit:false,
    },
    created: function () {
        var obj=this.change(window.location.search)
        if(obj.id){
            this.updateBookId=obj.id;
            this.isEdit=true;
            var book={
                bookId:obj.id
            }
            this.getBook(obj.id);

        }
        $.ajax({
            url: baseURL + 'sys/category/findAll',
            type: "POST",
            contentType: "application/json",
            success: function (result) {
                vm.sels = result.list
                console.log(vm.sels)
            },
            error: function (error) {
                alert('数据加载失败!');
            }
        })
    },
    filters:{
        pic: function(value) {
            if (
                value.backgroundImage === "url(undefined)" ||
                value.backgroundImage == "url()" ||
                value.backgroundImage == "url(null)"
            )
                return { backgroundImage: "url(../../img/zanwu.png" };
            return value;
        },
    },
    methods: {
        selectChange:function(val){
            for(var i=0;i<this.sels.length;i++ ){
                if(this.book.categoryid==this.sels[i].id){
                    this.book.categoryname=this.sels[i].categoryyname
                }
            }
},
        changeShoufa:function(index){
            this.book.publish=index;
            console.log(this.book.publish)
        },
        changeStatus:function(index){
            console.log(index)
            if(index){
                this.book.isserialize=true
            }else {
                this.book.isserialize=false
            }
        },
        // 获取路由ID
        change:function(str) {
            if(str == undefined) return
            str = str.substr(1)
            var arr = str.split("&"),
                obj = {},
                newArr = []
            arr.map(function(value,index,arr){
                newArr = value.split("=")
                if(newArr[0] != undefined) {
                    obj[newArr[0]] = newArr[1]
                }
            })
            return obj
        },
        getSectionList: function () {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showDom = "sectionList"
            window.location.href = 'section.html?id='+id
        },
        getVolumeList: function () {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showDom = "volumeList"
            window.location.href = 'volume2.html?id='+id
        },
        changeImage: function (e) {
            var file = e.target.files[0]
            var reader = new FileReader()
            var THIS = this
            reader.readAsDataURL(file)
            reader.onload = function (e) {
                // this.result 为图片
                THIS.upload(this.result)
            }
        },
        changeImage1: function (e) {
            var file = e.target.files[0]
            var reader = new FileReader()
            var THIS = this
            reader.readAsDataURL(file)
            reader.onload = function (e) {
                // this.result 为图片
                THIS.upload1(this.result)
            }
        },
        upload: function ($refs) {
            // 上传图片
            var THIS = this;
            if (this.$refs.file.files.length !== 0) {
                var image = new FormData()
                image.append('file', this.$refs.file.files[0])

                $.ajax({
                    url: '/youxiang/api/upload/up',
                    data: image,
                    // headers: {
                    //     "Content-Type": "multipart/form-data"
                    // },
                    cache: false,
                    processData: false,
                    contentType: false,
                    type: 'post',
                    success: function (result) {
                        console.log(result.url);
                        THIS.book.picurl = result.url;
                        THIS.imgUrl = result.url;
                    },
                    error: function (error) {
                        alert('图片上传失败!');
                    }
                })
            }
        },
        upload1: function ($refs) {
            // 上传图片
            var THIS = this;
            if (this.$refs.file1.files.length !== 0) {
                var image = new FormData()
                image.append('file', this.$refs.file1.files[0])

                $.ajax({
                    url: '/youxiang/api/upload/up',
                    data: image,
                    // headers: {
                    //     "Content-Type": "multipart/form-data"
                    // },
                    cache: false,
                    processData: false,
                    contentType: false,
                    type: 'post',
                    success: function (result) {
                        console.log(result.url);
                        THIS.book.picurl = result.url
                    },
                    error: function (error) {
                        alert('图片上传失败!');
                    }
                })
            }
        },
        query: function () {
            vm.reload();
        },
        backToBook:function(){
            window.location.href='authorBook.html'
        },
        add: function () {
            vm.showList = false;
            vm.showDom = 'addOrEdit'
            vm.title = "添加新书";
            vm.book = vm.book2;
        },
        add2: function () {
            vm.showList = false;
            vm.showDom = 'addOrEdit2'
            vm.title = "添加新书";
            vm.book = vm.book2;
        },
        updatebook: function () {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.showDom = 'addOrEdit'

            vm.title = "修改书籍";
            vm.getBook(id);
        },
        updatebookbyauthor: function () {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.showDom = 'addOrEdit2'

            vm.title = "修改书籍";
            vm.getBook(id);
        },
        updatesection: function () {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }

            vm.showList = false;
            vm.showDom = 'addSec'

            vm.title = "修改章节";
            // 应该在此处去查询
        },
        del: function () {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }
            confirm('删除将清理图书所有数据，确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/book/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert(r.msg, function () {
                                this.backToBook();
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        cateName: function () {
            vm.showList = true;
            vm.showDom = 'list'
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    'categoryid': this.book.categoryid
                },
                page: page
            }).trigger("reloadGrid");
        },
        saveOrUpdate: function (type) {
            console.log(vm.book)
            var THIS=this
            console.log(type)
            var arr;
            if(type){
                arr=type.split(',');
                vm.book.origin = arr[0];
                vm.book.source = arr[1];
            }else {
                vm.book.origin = type;
            }

            var url = "/sys/book/saveOrUpdate"
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.book),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function () {
                            THIS.$nextTick(function(){
                                THIS.backToBook()
                            })
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        getBook: function (id) {
            $.get(baseURL + "sys/book/findById/" + id, function (r) {
                console.log(r)
                vm.book = r.book;
            });
        },
        reload: function () {
            vm.showList = true;
            vm.showDom = 'list'

            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    'bookname': vm.q.bookname
                },
                page: page
            }).trigger("reloadGrid");
        }
    }
});